package com.abel.bigwater.dataserver.model.kming

import io.netty.buffer.ByteBuf
import java.util.*

class SessionStatus : TermData(KmingDataType.TermDummy, 3) {

    var back: BitSet? = null

    var hasNext = false

    var encrypted = false

    var serialNo: Int? = null

    override fun bufLength() = 3

    override fun parseBuf(buf: ByteBuf): Boolean {
        BitSet.valueOf(ByteArray(1).also { buf.readBytes(it) }).apply {
            back = this

            hasNext = get(0)
            encrypted = get(7)
        }

        serialNo = buf.readUnsignedShortLE()

        return true
    }

    override fun pack(buf: ByteBuf) {
        BitSet(8).apply {
            set(0, hasNext)
            set(7, encrypted)

            val array = toByteArray()
            buf.writeByte(if (array.isEmpty()) 0 else array[0].toInt())
        }

        buf.writeShortLE(serialNo!!.toInt())
    }

    override fun toString(): String {
        return "{next:$hasNext,secret:$encrypted,serial:$serialNo}"
    }
}